package com.baidu.ota.view;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.baidu.ota.R;

public class ConfirmFragment extends DialogFragment {
    private static IConfirmListener listener;

    interface IConfirmListener {
        void onConfirm(boolean yes);
    }

    public static void setListener(IConfirmListener l) {
        listener = l;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.DialogStyle);
        if (dialog.getWindow() != null) {
            dialog.getWindow().setDimAmount(0.65f);
        }
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        return inflater.inflate(R.layout.activity_confirm, container);
    }

    @Override
    public void onViewCreated(View view, Bundle bundle) {
        super.onViewCreated(view, bundle);

        TextView ignoreView = view.findViewById(R.id.ignore);
        TextView confirmView = view.findViewById(R.id.confirm);
        TextView infoTextview = view.findViewById(R.id.tv_info);
        TextView tvVersion = view.findViewById(R.id.tv_version);
        String info = getArguments().getString("info");
        String version = getArguments().getString("version");
        tvVersion.setText(getString(R.string.new_version_found) + "：" + version);
        infoTextview.setText(getString(R.string.latest_version) + "：" + info);
        View.OnClickListener listener = new ClickListener();
        ignoreView.setOnClickListener(listener);
        confirmView.setOnClickListener(listener);
    }

    private class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onConfirm(v.getId() == R.id.confirm);
            }
            listener = null;
            dismiss();
        }
    }
}
